package com.example.a80205050.myapplication.MyActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.a80205050.myapplication.Dao.OutaccountDao;
import com.example.a80205050.myapplication.Modle.Tb_outaccount;
import java.util.List;
/**
 * Created by 80205050 on 2017/10/31.
 */
public class Outaccountinfo extends Activity {
    public static  final String FLAG="id";//定义字符串常量，用来作为请求码
    ListView lvinfo;//创建ListView
    String strType=" ";//创建字符串，记录管理类型
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.outaccountinfo);//添加支出的布局文件
        lvinfo = (ListView) findViewById(R.id.lvoutaccountinfo);
        showInfo(R.id.btnoutinfo);//显示收入的信息
        lvinfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String strInfo=String.valueOf(((TextView)view).getText());//记录收入的信息
                String strid=strInfo.substring(0,strInfo.indexOf("|"));//截取信息的id
                Intent intent=new Intent(Outaccountinfo.this,InfoManage.class);//创建需要传到另一个活动的数据载体
                intent.putExtra(FLAG,new String[]{strid,strType});//存入数据
                startActivity(intent);//开启另一个活动
            }
        });
    }
    //当Activity重启时,加载信息
    @Override
    protected void onRestart() {
        super.onRestart();
        showInfo(R.id.btnoutinfo);//显示支出的信息
    }
    private void showInfo(int type){
        String[] strInfos=null;//定义字符串数组，用来存储收入的信息
        ArrayAdapter<String> arrayAdapter=null;//定义字符串适配器
        strType="btnoutinfo";
        OutaccountDao outccountDao=new OutaccountDao(Outaccountinfo.this);//定义支出操作数据库的对象
        List<Tb_outaccount> listinfos=outccountDao.getScrollData(0,(int)outccountDao.getCount());
        Log.i("listinfos",String.valueOf(listinfos.size()));
        strInfos=new String[listinfos.size()];//设置字符串的长度
        int strIndex=0;//定义开始的标识为0
        for(Tb_outaccount tb_outaccount:listinfos){
            strInfos[strIndex]=tb_outaccount.get_id()+"|"+tb_outaccount.getType()+" "
                    +String.valueOf(tb_outaccount.getMoney())+"元   "+tb_outaccount.getTime();
            strIndex++;
        }
        arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strInfos);
        lvinfo.setAdapter(arrayAdapter);//为listView设置数据源
    }
}
